<?php

/**
 * ProfessorController
 * 
 * @author Radovan Dvorsky
 * @version 20100403
 */

require_once 'Zend/Controller/Action.php';

class ProfessorController extends Zend_Controller_Action {
	
	/**
	 * Zobrazi vsetkych profesorov
	 */
	public function indexAction() {
		
		$professorsMapper = new Db_Mapper_Professor();
		
		/*
		 * Ak mozno, tak nacitaj z cache
		 */		 
		$cache = Zend_Registry::get('cache');
		
		
		/*
		 * Nastavi meno pod ktorym sa bude ukladat cache
		 */ 
		$cacheName = My_Support::getCacheName($this,'fetchAll');	
		
		if(!($professors = $cache->load($cacheName))){

			$order = $professorsMapper->getTable()->select()->order('lastname');
			/*
			 * Nacita profesorov z db
			 */
			$professors = $professorsMapper->fetchAll($order);
			
			/*
			 * Ulozi do cache
			 */
			$cache->save($professors,$cacheName,array(get_class($this)));
		}
		
		$this->view->professors = $professors;	
					
	}
	
	
	public function addAction(){
		
		$form = new Form_Professor_Add();		
		$this->view->form = $form;
		
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid ( $formData )) {
				try {
					
					
					/*
					 * Vloz do databaze
					 */
					$professorMapper = new Db_Mapper_Professor();
					
					$professorModel = new Db_Model_Professor($formData);
										
					$professorMapper->save($professorModel);					
					
					$cache = Zend_Registry::get('cache');
					$cache->remove(My_Support::getCacheName($this,'fetchAll'));
					
					$this->view->message = '<p class="success">Profesor pridaný</p>';
					
					
				} catch ( Zend_Exception $e ) {
					/*
					 * Ak je chyba vloz ju do logu
					 */					
					$log = Zend_Registry::get('log');    		
    				$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
    				
					$this->view->message = '<p class="error">Profesora sa nepodarilo pridať (viz. log)</p>';
				}
			}
		}
	}
	
	
	public function addcsvAction(){

		$formCsv = new Form_Professor_Csv();
		$this->view->form = $formCsv;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();			
			if($formCsv->isValid($formData)){				
					try{						
						
						$professorMapper = new Db_Mapper_Professor();
						$adapter = $professorMapper->getTable()->getAdapter();
											
						$adapter->beginTransaction();
						
						$csvImport = new My_Csv_Import();
						$csvImport->setPattern(new My_Pattern('[:title_before][:firstname][:lastname][:title_behind]'));
						$csvImport->setFile($_FILES['file']);					
						$data = $csvImport->parseToArray();						
						
						$professorMapper->saveArray($data);						
						
						$cache= Zend_Registry::get('cache');						
						$cache->remove(My_Support::getCacheName($this,'fetchAll'));
						
						$adapter->commit();
						
						$this->_redirect('/professor/');
							
					} catch(Zend_Exception $e){
						
						$adapter->rollBack();
						/*
					 	* Ak je chyba vloz ju do logu
					 	*/					
						$log = Zend_Registry::get('log');    		
    					$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);				
						
						$this->view->message = 'Profesorov sa nepodarilo vložiť (viz. log)';
					} 			
			}
		}
	}
	
	/**
	 * Uprava informacii o profesorovi	 
	 */
	public function editAction(){
		
		/*
		 * Ziskaj pid z url
		 */
		$pid = (int) $this->getRequest()->getParam('pid');
		
		$professorMapper = new Db_Mapper_Professor();
		$editForm = new Form_Professor_Add();
		
		/*
		 * Zmen fieldset decorator formulara
		 */
		$editForm->addDecorator('fieldset',array('legend' => 'Upraviť profesora'));
		
		/*
		 * Vyber z db profesora na zaklade pid
		 */
		$professorModel = $professorMapper->fetchRow($professorMapper->getTable()->select()->where('pid = ?',$pid));
		
				
		/*
		 * Vypln formular datami z db
		 * pozn. mena poloziek formulara a mena riadkov v db musia byt rovnake
		 */	
		
		$editForm->populate($professorModel->toArray());	
		
		/*
		 * Prirad formular do view
		 */
		$this->view->form = $editForm;
		
		/*
		 * Ak je odoslany $_POST
		 */
		if($this->getRequest()->isPost()){
			/*
			 * Vloz odoslane $_POST do $formData
			 */
			$formData = $this->getRequest ()->getPost ();
			/*
			 * Skontroluj ci je formular validny
			 */
			if ($editForm->isValid ( $formData )) {
				try {
															
					/*
					 * Vloz upraveneho profesora do db
					 */
					$professorMapper->save(new Db_Model_Professor($formData));
					
					/*
					 * Vymaz cache
					 */
					$cache = Zend_Registry::get('cache');
					$cache->remove(My_Support::getCacheName($this,'fetchAll'));				
					/*
					 * Odstrani vsetku cache pre commission controller
					 */
					$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('CommissionController'));					
					
					/*
					 * Presmeruj na index
					 */
					$this->_redirect ( '/professor/' );
				
				} catch ( Zend_Db_Exception $e ) {
					/*
					 * Chybu zapis do logu
					 */
					$log = Zend_Registry::get('log');    		
		    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					$this->view->message = 'Profesora sa nepodarilo upraviť';
					
				}
			}			
		}		
	}
	
	public function deleteAction(){		
		
		/*
		 * Vypnut view renderer
		 */
		$this->_helper->viewRenderer->setNoRender(true);
		
		/*
		 * Vypnut layout
		 */
		$this->_helper->layout()->disableLayout();
		
		/*
		 * Ziskaj hodnoty (json)
		 */
		$pidPost = $this->getRequest()->getParam('id');
		
		if(!empty($pidPost)){
			/*
			 * Vytvor pole
			 */
			$pids = explode(',',$pidPost);			
		
			try{
				
				$professorMapper = new Db_Mapper_Professor();	
				
				/*
				 * Zacni transakciu
				 */
				$adapter = $professorMapper->getTable()->getAdapter();
				$adapter->beginTransaction();
				
				/*
				 * Odstran z db podla pid
				 */
				foreach($pids as $pid){
					$professorMapper->delete(
						new Db_Model_Professor(array('pid' => $pid))
					);		
				}
			
				/*
				 * Vymaz cache
				 */
				$cache = Zend_Registry::get('cache');
				$cache->remove(My_Support::getCacheName($this,'fetchAll'));				
				/*
				 * Odstrani vsetku cache pre commission controller
				 */
				$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('CommissionController'));
				
				/*
				 * Pridaj zmeny
				 */
				$adapter->commit();
				
				echo '<p class="success">Profesor(i) vymazaní</p>'; 
				
			} catch(Zend_Db_Exception $e){
				/*
				 * Ak chyba tak nerob nic
				 */
				$adapter->rollBack();
				echo '<p class="error">Profesorov sa nepodarilo odstrániť</p>';
				
				/*
				 * Chybu zapis do logu
				 */
				$log = Zend_Registry::get('log');    		
	    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
			}
		}
		else {
			$this->_redirect('/professor/');
		}		
	}
}